<template>
  <div class='car-detail'>
    <Form :formData="formData" :formParam="formParam"></Form>
    <Buttons class="margintop80" :buttonData="buttonData" ></Buttons>
  </div>
</template>
<script>
import Form from "../../common/form"
import Buttons from '../../common/button.vue'
export default {
  components: {Form,Buttons},
  name: "",
  data() {
    return {
      formData:[
        {
          title:{
            show:false,
          },
          children:[
            {type:"input",label:"车型",key:"carModel"},
            {type:"input",label:"车牌号",key:"plateNum",},
            {type:"number",label:"车架号码",key:"frameNum",otherText:"后6位"},
            {type:"number",label:"发动机号",key:"engineNum",otherText:"后6位"}
          ],
        },{
          title:{
            show:false,
          },
          children:[
            {type:"date",label:"保险购买日期",key:"insuranceStart",},
            {type:"number",label:"上次保养公里数",key:"lastMaintainKm",},
            {type:"number",label:"保养间隔公里数",key:"maintainIntervalKm",},
            {type:"date",label:"上次保养时间",key:"lastMaintainTime",}
          ],
        },{
          title:{
            show:false,
          },
          children:[
            {type:"date",label:"首次上牌时间",key:"firstBoardTime",}
          ],
        },
      ],
      formParam:{
        carModel:"",
        plateNum:"",
        frameNum:"",
        engineNum:"",
        insuranceStart:"",
        lastMaintainKm:"",
        maintainIntervalKm:"",
        lastMaintainTime:"",
        firstBoardTime:"",
      },

       buttonData:[
        {
          label:"保存",
          click:this.save,
        },
      ]
    };
  },
  methods: {
    formalTime(time){
      var time=new Date(time)
      return time.getFullYear()+"-"+(time.getMonth()+1)+"-"+time.getDate()
    },
    save(){
      var carId=sessionStorage.getItem("userId")
      var params={
        id:carId,
      }
      _g.apiPost("trafficReport/save.do",_g.assign(this.formParam,params)).then(res=>{
        _g.toMessage(res)
        if(res.status==0){
          this.$router.go(-1)
        }
      })
      console.log("click----保存")
    },
    getList(){
      var carInfo = JSON.parse(sessionStorage.getItem("carInfo")=="undefined"?"{}":sessionStorage.getItem("carInfo"))
      
      // return
      var formalA=["insuranceStart","lastMaintainTime","firstBoardTime"]
      for(var key in this.formParam){
        this.formParam[key]=carInfo[key]
        if(formalA.includes(key)){
          this.formParam[key]=this.formalTime(this.formParam[key]=="NaN-NaN-NaN"?"":this.formParam[key])
        }
      }
      console.log(this.formParam)
    }
  },
  created() {
    this.getList()
  }
};
</script>
<style lang="scss" scoped>
.car-detail {
  background-color: #fff;
}
.margintop80{
  padding: 80px 15px 0;
}
</style>